/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package talvan;

import java.util.ArrayList;
import java.util.Date;

/**
 * Hetta er ein klassi sum heldur methods sum MAIN GUI skal brúka
 * @author DJH
 */
public class SQLCallerMain {
    private DbHandler dbh;
    private int userID;
    
    public SQLCallerMain(DbHandler dbh, int userID)
    {
        this.dbh = dbh;
        this.userID = userID;
    }
    
    /**
     * Metoda sum sendur ein sql statement og retrievar ArrayList við Fundim hjá bríkarinum
     * 
     * 
     * @return ArrayList<Fund> fundum hjá hesumbrúkarinum
     */
    public ArrayList<Meeting> getMeetings() 
    {
        ArrayList<Meeting> resultList = new ArrayList<Meeting>();
        java.sql.ResultSet rs = null;
        try {
            if (dbh.getConnection() != null) {
                String sql = "USE TALVAN; ";
                sql += "SELECT Meeting.name as name, Meeting.details as details, Employe.firstName as fornavn, Meeting.timee as time, Meeting.id as id "
                     + "FROM Meeting, Participant, Employe "
                     + "WHERE Participant.employe_id = '" + userID + "' AND Participant.meeting_id = Meeting.id AND  Employe.id = Meeting.creator_id "
                     + "ORDER BY time ASC";
                
                java.sql.Statement stmt = dbh.getConnection().createStatement();
                rs = stmt.executeQuery(sql);
                //loop sum creatar objects og addar í array ella Arraylist?
                while (rs.next()) {
                    Meeting newFundur = new Meeting();
                    newFundur.setName(rs.getString("name"));
                    newFundur.setDescription(rs.getString("details"));
                    newFundur.setStovnari(rs.getString("fornavn"));
                    String stringDate = rs.getString("time");
                    Date date = convertToDate(stringDate);
                    newFundur.setTime(date);
                    int id = Integer.parseInt(rs.getString("id"));
                    newFundur.setMeetingID(id);
                    resultList.add(newFundur);
                }
                rs.close();
                rs = null;
                dbh.closeConnection();
            } else {
                throw new Exception("Error: No active Connection");
            }
        } catch (Exception e) {
            //throw new Exception("SQL statement í DataHandler (getMeetings metodan)");
        }
        return resultList;
    }
    
    
    
    /**
     * Metoda sum heintar øll events sum brúkarin hevur fingi inbjóðing til
     * @return listi við events
     */
    public ArrayList<Event> getEvents() 
    {
        ArrayList<Event> resultList = new ArrayList<Event>();
        java.sql.ResultSet rs = null;
        try {
            if (dbh.getConnection() != null) {
                String sql = "USE TALVAN; ";
                sql += "SELECT Eevent.name as name, Eevent.details as details, Employe.firstName as fornavn, Eevent.id as id "
                     + "FROM Eevent, Invitation, Employe "
                     + "WHERE Invitation.employe_id = '" + userID + "' AND Invitation.id = Eevent.id AND  Employe.id = Eevent.creator_id ";
                    // "ORDER BY time ASC";
                
                java.sql.Statement stmt = dbh.getConnection().createStatement();
                rs = stmt.executeQuery(sql);
                //loop sum creatar objects og addar í array ella Arraylist?
                while (rs.next()) {
                    Event newEvent = new Event();
                    newEvent.setName(rs.getString("name"));
                    newEvent.setDescription(rs.getString("details"));
                    newEvent.setStovnari(rs.getString("fornavn"));
                    //String stringDate = rs.getString("time");
                    //Date date = convertToDate(stringDate);
                    //newFundur.setTime(date);
                    int id = Integer.parseInt(rs.getString("id"));
                    newEvent.setEventID(id);
                    resultList.add(newEvent);
                }
                rs.close();
                rs = null;
                dbh.closeConnection();
            } else {
                throw new Exception("Error: No active Connection");
            }
        } catch (Exception e) {
            //throw new Exception("SQL statement í DataHandler (getMeetings metodan)");
        }
        return resultList;
    }
    
    
    
    
    private Date convertToDate(String stringDate)
    {
        int year = Integer.parseInt(stringDate.substring(0, 4));
        int month = Integer.parseInt(stringDate.substring(5, 7));
        int day = Integer.parseInt(stringDate.substring(8, 10));
        int hour = Integer.parseInt(stringDate.substring(11, 13));
        int min = Integer.parseInt(stringDate.substring(14, 16));
                    
        Date date = new Date(year, month, day, hour, min);
        return date;
    }
    
    
}
